﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WJP.Logging
{
    [Serializable]
    public class StreamLogger : AbstractLogger, ILogger
    {
        public StreamLogger(string name) : base(name, LogLevel.Debug)
        {
            
        }

        public override ILogger CreateChildLogger(string loggerName)
        {
            throw new NotSupportedException("StreamLogger不支持子日志记录器");
        }

        protected override void Log(LogLevel loggerLevel, string loggerName, string message, Exception exception)
        {
            using (StreamWriter streamWriter = new StreamWriter("sys.log", true))
            {
                streamWriter.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                streamWriter.WriteLine("[{0}] '{1}' {2} {3}", loggerLevel, loggerName, System.Environment.NewLine, message);
                if (exception != null)
                {
                    streamWriter.WriteLine("[{0}] '{1}' {2} {3} {4} {5} {6}",
                        loggerLevel,
                        loggerName,
                        exception.GetType().FullName,
                        System.Environment.NewLine,
                        exception.Message,
                        System.Environment.NewLine,
                        exception.StackTrace
                    );
                }
            }
        }
    }
}
